package com.share.shareplatform.pojo;


import com.share.shareplatform.common.state.OrderState;
import com.share.shareplatform.common.state.OrderType;
import com.share.shareplatform.pojo.exception.LogicException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.Min;
import java.sql.Timestamp;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Order {
    private Long id;
    private Long userId;
    private Long goodsId;

    private Double pay; //总金额

    @Min(value = 1,message = "购买数量不能低于1")
    private Integer buyNumber;//购买数量

    private String state;//订单状态

    @Length(max = 100,message = "订单备注过长")
    private String message;//订单备注

    private Timestamp startTime;

    private Timestamp endTime;

    @Min(value = 1,message = "请指定正确的租赁天数")
    private Integer dayNumber;//租赁天数

    private String type;//订单类型

    private Goods goods;
    private User user;

    public static void setInit(Order order,Goods goods) {
        long time = System.currentTimeMillis();
        order.setStartTime(new Timestamp(time));

        if(order.getType().equals(OrderType.RENT.getType())){
            if(order.getDayNumber()==null){
                throw new LogicException("请指定租赁天数");
            }
            order.setEndTime(new Timestamp(time+order.getDayNumber()*24*60*60*1000));
            order.setPay(order.getBuyNumber()*goods.getRentPrice()* order.getDayNumber());
        }
        else
            order.setPay(order.getBuyNumber()*goods.getPrice());
        order.setState(OrderState.WAITING_FOR_PAYMENT.getState());
    }

    public static boolean isDeletable(Order order){
        /*if(order.getState().equals(OrderState.WAITING_FOR_PAYMENT.getState())){
            return false;
        }*/
        return true;
    }

    public static void update(Order oldOrder,Order newOrder) throws LogicException {

        oldOrder.setState(newOrder.getState());
        oldOrder.setMessage(newOrder.getMessage());
    }
}
